import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter2/colors.dart';
import 'package:flutter2/dialog/dialog_permission.dart';
import 'package:flutter2/http/api.dart';
import 'package:flutter2/http/dio_manager.dart';
import 'package:flutter2/model/user_entity.dart';
import 'package:flutter2/ui/home.dart';
import 'package:flutter2/utils/sp.dart';
import 'package:flutter2/utils/string.dart';
import 'package:flutter2/utils/system_bar.dart';
import 'package:flutter2/widget/base/check_box.dart';
import 'package:flutter2/widget/base/normal_button.dart';
import 'package:flutter2/widget/base/normal_edit.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  String info = "";
  bool check = true;

  final _phoneController = TextEditingController();
  final _codeController = TextEditingController();

  @override
  void initState() {
    super.initState();
    bool show = SPUtil.getBool("showPermission", false);
    if (!show) {
      SPUtil.putBool("showPermission", true);
      SmartDialog.show(
        alignment: Alignment.bottomCenter,
        builder: (context) {
          return SafeArea(child: DialogPermission());
        },
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion(
      value: systemUiOverlayStyle,
      child: Scaffold(
        body: SingleChildScrollView(
          physics: BouncingScrollPhysics(),
          child: Column(
            children: [
              Stack(
                alignment: AlignmentDirectional.bottomCenter,
                clipBehavior: Clip.none,
                children: [
                  Image.asset(
                    "images/bg_login_top.png",
                    width: double.infinity,
                  ),
                  Positioned(
                    bottom: -40,
                    child: Image.asset(
                      "images/ic_splash.png",
                      width: 80,
                      height: 80,
                    ),
                  ),
                ],
              ),
              SizedBox(
                height: 40,
              ),
              Padding(
                padding: const EdgeInsets.all(16),
                child: Text(
                  textAlign: TextAlign.center,
                  "número de tu WhatsApp para comunicación rápida y segura. ¡Tu informaciónestá protegida!]",
                  style: TextStyle(
                    fontSize: 12,
                    color: Color(0x660C381E),
                  ),
                ),
              ),
              Container(
                margin: EdgeInsets.symmetric(horizontal: 16),
                padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
                decoration: BoxDecoration(
                  color: Color(0x0A0C381E),
                  borderRadius: BorderRadius.all(
                    Radius.circular(10),
                  ),
                ),
                child: Row(
                  children: [
                    Text(
                      "+52",
                      style: TextStyle(fontSize: 12),
                    ),
                    SizedBox(
                      width: 10,
                    ),
                    Expanded(
                      child: NormalEdit(
                        inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                        controller: _phoneController,
                        maxLength: 10,
                        inputType: TextInputType.number,
                        fontSize: 14,
                        fontColor: mainText2,
                        hintText: "Ingrese número telefónico",
                        hintFontColor: mainText3,
                        onChanged: (value) {
                          setState(() {});
                        },
                      ),
                    ),
                  ],
                ),
              ),
              SizedBox(
                height: 20,
              ),
              Container(
                margin: EdgeInsets.symmetric(horizontal: 16),
                padding: EdgeInsets.symmetric(horizontal: 20, vertical: 5),
                decoration: BoxDecoration(
                  color: Color(0x0A0C381E),
                  borderRadius: BorderRadius.all(
                    Radius.circular(10),
                  ),
                ),
                child: NormalEdit(
                  inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                  controller: _codeController,
                  maxLength: 4,
                  inputType: TextInputType.number,
                  fontSize: 14,
                  fontColor: mainText2,
                  hintFontColor: mainText3,
                  hintText: "Ingrese código de verificación",
                  onChanged: (value) {
                    setState(() {});
                  },
                ),
              ),
              NormalButton(
                height: 60,
                width: double.infinity,
                margin: EdgeInsets.symmetric(horizontal: 16, vertical: 20),
                text: "Inicio",
                fontSize: 16,
                enable: _phoneController.text.isNotEmpty && _codeController.text.isNotEmpty && check,
                click: _login,
              ),
              Padding(
                padding: const EdgeInsets.only(left: 16, right: 16),
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    CheckBox(
                      check: check,
                      checkIcon: "cb_checked".toImage(),
                      uncheckIcon: "cb_unchecked".toImage(),
                      onChange: (check) {
                        setState(() {
                          this.check = check;
                        });
                      },
                    ),
                    SizedBox(
                      width: 10,
                    ),
                    Expanded(
                      child: Text.rich(
                        TextSpan(
                          children: [
                            TextSpan(
                              text: "Al iniciar sesión, aceptas nuestros_ ",
                              style: TextStyle(
                                fontSize: 12,
                                color: mainText3,
                              ),
                            ),
                            TextSpan(
                              text: "términos de servicio",
                              style: TextStyle(
                                fontSize: 12,
                                color: btnColor,
                                decoration: TextDecoration.underline,
                                decorationColor: btnColor,
                              ),
                            ),
                            TextSpan(
                              text: " y ",
                              style: TextStyle(
                                fontSize: 12,
                                color: mainText3,
                              ),
                            ),
                            TextSpan(
                              text: "política de privacidad.",
                              style: TextStyle(
                                fontSize: 12,
                                color: btnColor,
                                decoration: TextDecoration.underline,
                                decorationColor: btnColor,
                              ),
                            ),
                          ],
                        ),
                      ),
                    ),
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  _login() {
    var map = getParams();
    map["basketballcautionwaist"] = _phoneController.text;
    map["dimensiongobut"] = _codeController.text;
    DioManager.request<UserEntity>(
      path: HttpApi.login,
      data: map,
      onSuccess: (data) {
        data?.phone = _phoneController.text;
        SPUtil.putObject("user", data?.toJson());
        Get.off(HomePage());
      },
    );
  }
}
